#ifndef FPLAYER_MESSAGEQUEUE_H
#define FPLAYER_MESSAGEQUEUE_H

#include "Message.h"
#include "concurrent/Event.h"

/**
 * author: x-fan
 * date: 2024/1/9
 * desc: 消息队列
 */
class MessageQueue : public IBasic {
private:
  //队首
  MessageNode *_pFirst = nullptr;
  //队尾
  MessageNode *_pLast = nullptr;
  //队列长度
  int32_t _NbSize = 0;
  //队列最大长度
  int32_t _MaxSize = 0;

  bool _AbortRequest = false;

  Event event;

public:
  MessageQueue();

  explicit MessageQueue(int32_t maxSize);

  virtual ~MessageQueue() noexcept;

  /**
   * 释放所有的消息
   */
  void flush();

  /**
   * 获取队列长度
   * @return 队列长度
   */
  int32_t getSize();

  /**
   * 中断消息存储
   */
  void abort();

  /**
   * 消息入列
   * @param pMsg 消息体
   * @return 0 is Success
   */
  bool enqueueMessage(Message *pMsg);

#define IMMEDIATE -1

  /**
   * 消息出列
   * @param waitMills 等待时长 单位毫秒
   *    IMMEDIATE:立即返回
   * @return 消息体
   */
  Message *dequeueMessage(const int32_t waitMills);

  const char *getClassName() override;

  void toString() override;

};


#endif //FPLAYER_MESSAGEQUEUE_H
